import unittest
import requests
import json

from api.login import LoginAPI
from parameterized import parameterized
from tools.dbutil import DBUtil



def build_data():
    sql = "select *from t_login"
    db_data = DBUtil.exe_sql(sql)
    print(f"db_data: {db_data}")
    test_data = []
    for case_data in db_data:
        username = case_data[2]
        password = case_data[3]
        verify_code = case_data[4]
        status_code = case_data[5]
        status = case_data[7]
        msg = case_data[8]
        test_data.append((username, password, verify_code, status_code, status, msg))
    print("test_data=".format(username, password, verify_code, status_code, status, msg))
    return test_data


class TestLogin3(unittest.TestCase):

    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.Session()

    @parameterized.expand(build_data())
    def test01_login(self, username, password, verify_code, status_code, status, msg):
        response1 = self.login_api.get_verify_code(self.session)
        self.assertEqual(status_code, response1.status_code)
        self.assertIn("image", response1.headers.get("Content-Type"))

        response2 = self.login_api.login(self.session, username, password, verify_code)
        print(response2.json())
        self.assertEqual(status_code, response2.status_code)
        self.assertEqual(status, response2.json().get("status"))
        self.assertIn(msg, response2.json().get("msg"))

    def tearDown(self):
        if self.session:
            self.session.close()











